﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using LTP.Accounts.Bus;

namespace TCS.Web.Admin.NewsClassManage
{
    public partial class indexR : System.Web.UI.Page
    {
        TCS.BLL.NewsManage.NewsClass bll = new TCS.BLL.NewsManage.NewsClass();
        public static string id = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
                {
                    id = Request.QueryString["id"].ToString();
                }
                BindData();
            }
        }

        #region BindData
        private void BindData()
        {
            #region 权限检查
            if (!Context.User.Identity.IsAuthenticated)
            {
                return;
            }

            #endregion

            string strWhere = " classid=" + id;
            if (Session["strWhereCategory2"] != null && Session["strWhereCategory2"].ToString() != "")
            {
                strWhere += Session["strWhereCategory2"].ToString();
            }
            DataSet ds = new DataSet();
            ds = bll.GetList(strWhere);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Label1.Visible = false;
                DataView dv = ds.Tables[0].DefaultView;
                gridView.DataSource = dv;
                gridView.DataBind();

                //分页
                int rows_Count = ds.Tables[0].Rows.Count;
                int page_Size = gridView.PageSize;
                int page_Count = gridView.PageCount;
                int page_Current = gridView.PageIndex + 1;

                lblRowsCount.Text = rows_Count.ToString();
                lblPageCount.Text = page_Count.ToString();
                lblCurrentPage.Text = page_Current.ToString();


                #region 显示页导航

                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
                if (gridView.PageIndex == 0)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    if (gridView.PageCount == 1)
                    {
                        btnLast.Enabled = false;
                        btnNext.Enabled = false;
                    }
                }
                else if (gridView.PageIndex == gridView.PageCount - 1)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else
            {
                gridView.DataSource = ds;
                gridView.DataBind();
                Label1.Visible = true;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }

                #endregion

        }
        #endregion

        #region 页导航事件
        public void NavigateToPage(object sender, CommandEventArgs e)
        {
            btnFirst.Enabled = true;
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            string pageinfo = e.CommandArgument.ToString();
            switch (pageinfo)
            {
                case "Prev":
                    if (gridView.PageIndex > 0)
                    {
                        gridView.PageIndex -= 1;

                    }
                    break;
                case "Next":
                    if (gridView.PageIndex < (gridView.PageCount - 1))
                    {
                        gridView.PageIndex += 1;

                    }
                    break;
                case "First":
                    gridView.PageIndex = 0;
                    break;
                case "Last":
                    gridView.PageIndex = gridView.PageCount - 1;
                    break;
            }
            if (gridView.PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (gridView.PageCount == 1)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (gridView.PageIndex == gridView.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            BindData();
        }
        #endregion

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string Id = gridView.DataKeys[e.RowIndex].Value.ToString();
            bll.Delete(int.Parse(Id));
            BindData();
        }

        protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //页导航
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                TableRow row = e.Row.Controls[0].Controls[0].Controls[0] as TableRow;
                foreach (TableCell cell in row.Cells)
                {
                    Control lb = cell.Controls[0];
                    if (lb is Label)
                    {
                        Label lblpage = (Label)lb;
                        lblpage.ForeColor = System.Drawing.ColorTranslator.FromHtml("#afc4e3");
                        lblpage.Font.Bold = true;
                        lblpage.Text = "[" + lblpage.Text + "]";
                    }
                    else
                        if (lb is LinkButton)
                        {
                            LinkButton lblpage = (LinkButton)lb;
                            lblpage.Font.Bold = true;
                            lblpage.Text = "[" + lblpage.Text + "]";
                        }
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string title = Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString();
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#e6eff8';this.style.cursor='hand';");
                //当鼠标移走时还原该行的背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }

        }


        /// <summary>
        /// 所属区县
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        public string Getqy(object ob)
        {
            TCS.Model.Gsp_Administrative Class = new TCS.BLL.Gsp_Administrative().GetModel(int.Parse(ob.ToString()));

            return Class.AddName;
        }

        protected void btn_Search_Click(object sender, ImageClickEventArgs e)
        {
            Session["strWhereCategory2"] = "";
            string startime = this.txtStar.Text.Trim();
            string endtime = this.txtend.Text.Trim();
            if (startime.Trim() != "")
            {
                Session["strWhereCategory2"] += "and createtime>'" + startime + "'";
            }
            if (endtime.Trim() != "")
            {
                Session["strWhereCategory2"] += "and createtime<'" + endtime + "'";
            }
            BindData();
        }

        protected string GetNum(string n)
        {
            if ((this.gridView.PageIndex + 1) > 1)
            {
                return Convert.ToString((gridView.PageIndex) * gridView.PageSize + (int.Parse(n) + 1));
            }
            else
            {
                return Convert.ToString(int.Parse(n) + 1);
            }
        }

    }
}

